package com.dgw.springbootrabbitmq.receiver;

import com.dgw.springbootrabbitmq.config.TaskConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * @program: spring-boot-rabbitmq
 * @description:
 * @author: Mr.Dai
 * @create: 2020-02-20 21:30
 **/
@Component
@RabbitListener(queues = TaskConfig.TASK_QUEUE_NAME)
public class TaskReceiver {
    
  /*  private final int instance;

    public TaskReceiver(int i) {
        this.instance = i;
    }*/
    private static final Logger log = LoggerFactory.getLogger(TaskReceiver.class);
    @RabbitHandler
    public void receive(String in) throws InterruptedException {
        // 用于处理多个任务
        StopWatch watch = new StopWatch();
        watch.start();
        log.info("简单消息接受成功，参数是："+in);
        doWork(in);
        watch.stop();
        System.out.println("instance "+
                " [x] Done in " + watch.getTotalTimeSeconds() + "s");
    }

    private void doWork(String in) throws InterruptedException {
        for (char ch : in.toCharArray()) {
            if (ch == '.') {
                Thread.sleep(1000);
            }
        }
    }
}
